package cn.janus.dao.persist.respository.impl;

import cn.janus.dao.persist.mapper.BillMapper;
import cn.janus.dao.persist.respository.IBillRepository;
import cn.janus.pojo.po.BillPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public class BillRepository implements IBillRepository {
    private final BillMapper billMapper;

    public BillRepository(BillMapper billMapper) {
        this.billMapper = billMapper;
    }

    @Override
    public void record(List<BillPO> billPOS) {
        for (BillPO billPO : billPOS) {
            billMapper.insert(billPO);
        }
    }

    @Override
    public List<BillPO> billDetail() {
        QueryWrapper<BillPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("LIMIT 8")
                .orderByDesc("time", "amount");
        return billMapper.selectList(queryWrapper);
    }

    @Override
    public List<BillPO> selectForCategory() {
        QueryWrapper<BillPO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("category")
                .groupBy("category");
        return billMapper.selectList(queryWrapper);
    }

    @Override
    public List<BillPO> selectIncome() {
        QueryWrapper<BillPO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .in("category", "收入")
                .orderByDesc("amount");
        return billMapper.selectList(queryWrapper);
    }

    @Override
    public List<BillPO> selectExpend() {
        QueryWrapper<BillPO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .notIn("category", "收入")
                .orderByDesc("amount");
        return billMapper.selectList(queryWrapper);
    }

    @Override
    public List<BillPO> statisticCurrentRepository() {
        QueryWrapper<BillPO> wrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        now.withDayOfMonth(1);
        wrapper.ge("time", now);
        List<BillPO> billPOS = billMapper.selectList(wrapper);
        return billPOS;
    }

    @Override
    public List<BillPO> billForMonthRepository(LocalDate firstDay, LocalDate endDay) {
        QueryWrapper<BillPO> wrapper = new QueryWrapper<>();
        wrapper.ge("time", firstDay)
                .le("time", endDay)
                .select("time", "amount", "category", "purpose");
        return billMapper.selectList(wrapper);
    }

    @Override
    public List<BillPO> billTripRepository() {
        QueryWrapper<BillPO> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("remark");
        return billMapper.selectList(wrapper);
    }
}
